<?php
namespace App\Utils\Service\lak;
use App\Utils\Service\Ly\JwtServer;
use App\Models\Ly\wxLogin as Login;
class WxLogin
{
    public static function WxUserInfoLogin($code,$nickName,$avatarUrl){
        $appid = config('wxlogin.appid');
        $secret = config('wxlogin.appsecret');
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $res = json_decode(file_get_contents($url),true);
        $info = [
            'nickName'=>$nickName,
            'avatarUrl'=>$avatarUrl,
            'openid'=>$res['openid'],
            'session_key'=>$res['session_key']
        ];
        $user = Login::getOne($info);
        if (empty($user)){
            Login::Creat($info);
        }
        $token = JwtServer::encode($user['id']);
        return $token;
    }

}
